package com.haojiu.controller;

import com.github.pagehelper.PageInfo;
import com.haojiu.pojo.Book;
import com.haojiu.pojo.LendList;
import com.haojiu.pojo.Notice;
import com.haojiu.service.BookService;
import com.haojiu.util.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:我叫浩九
 * @date：2021/8/24 19:25
 * create in RuGao JiangSu
 */
@Controller
public class BookController {
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/bookIndex")
    public ModelAndView queryAllBook(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        List<Book> list = bookService.queryAllBook(page, size);
        PageInfo<Book> pageInfo = new PageInfo<>(list);
        mv.addObject("pageInfo", pageInfo);
        HttpSession session = request.getSession();
        String userType = (String) session.getAttribute("userType");
        if(userType.equals("manager")){
            mv.setViewName("book/allBook");
        }else {
            mv.setViewName("book/allBook2");
        }
        return mv;
    }

    @RequestMapping("/login/toAddBook")
    public String toAddPaper() {
        return "book/addBook";
    }

    @RequestMapping("/login/addBook")
    public String addPaper(Book books) {
        System.out.println(books);
        bookService.addBook(books);
        return "redirect:/bookIndex";
    }

    @RequestMapping("/login/toUpdateBook")
    public String toUpdateBook(Model model, int id) {
        Book books = bookService.queryBookById(id);
        System.out.println(books);
        model.addAttribute("book", books);
        return "book/updateBook";
    }

    @RequestMapping("/login/updateBook")
    public String updateBook(Model model, Book book) {
        System.out.println(book);
        bookService.updateBook(book);
        Book books = bookService.queryBookById(book.getBookID());
        model.addAttribute("books", books);
        return "redirect:/bookIndex";
    }

    @RequestMapping("/login/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/bookIndex";
    }

    @RequestMapping("/login/search2")
    public String queryBookById(@RequestParam("keyword") int id,Model model) {
        System.out.println(id);
        Book book = bookService.queryBookById(id);
        if (book != null) {
            List<Book> list = new ArrayList<>();
            list.add(book);
            model.addAttribute("one",list);
            return "book/oneBook";
        } else {
            return "redirect:/bookIndex";
        }
    }

    /*@RequestMapping("/search22")
    public ModelAndView queryBookById(@RequestParam("keyword") int id) {
        System.out.println(id);
        ModelAndView mv = new ModelAndView();
        Book book = bookService.queryBookById(id);
        List<Book> list = new ArrayList<>();
        list.add(book);
        PageInfo<Book> pageInfo = new PageInfo(list);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("book/oneBook");
        return mv;
    }*/

    @RequestMapping("/login/search")
    public String queryBookByName(@RequestParam("keyword") String bookName) {
        // System.out.println(id);
        //if (bookName)
        Book book = bookService.queryBookByName(bookName);
        if (book == null) {
            return "redirect:/bookIndex";
        } else {
            return "redirect:/bookIndex";
        }
    }

    @RequestMapping("/login/exportBook")
    public void export(HttpServletRequest request, HttpServletResponse response){
        List<Book> bookList = bookService.queryAllBook();
        ExportExcel<Book> ee= new ExportExcel<Book>();
        String[] headers = {"图书编号","书名","图书数量","作者","图书类型","价格","语言"};
        String fileName = "阅读之家图书信息excel表";
        ee.exportExcel(headers,bookList,fileName,response);
    }
}
